package socket.handler;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import tnt.sudoku_client_android.HomeActivity;
import tnt.sudoku_client_android.LoginActivity;
import tnt.sudoku_client_android.LoginSuccessActivity;
import util.ConnectUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import message.Message;

public class ClientSocketHandler implements SocketHandler {
	private Socket socket;
	private ObjectInputStream in;
	private ObjectOutputStream out;
	private Context context;
	private HandlerMessage handlerMessage;
	public ClientSocketHandler(Socket socket, ObjectInputStream in,
			ObjectOutputStream out) {
		super();
		this.socket = socket;
		this.in = in;
		this.out = out;
		handlerMessage=new HandlerMessage();
	}

	public ClientSocketHandler(Socket socket, ObjectInputStream in,
			ObjectOutputStream out, Context context) {
		super();
		this.socket = socket;
		this.in = in;
		this.out = out;
		this.context = context;
		handlerMessage=new HandlerMessage();
	}
	@Override
	public Message readSocket() {
		// TODO Auto-generated method stub
		Message message=null;
		try {
			message=(Message) in.readObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
	}

	@Override
	public boolean writeSocket(Message message) {
		// TODO Auto-generated method stub
		try {
			out.writeObject(message);
			out.flush();
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public void closeSocket() throws IOException {
		// TODO Auto-generated method stub
		in.close();
		out.close();
		socket.close();
		ConnectUtil.disconnect();
	}

	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub
		Log.e("Test", message.getType());
		android.os.Message msg=new android.os.Message();
		msg.obj=message;
		handlerMessage.sendMessage(msg);
	}

	@Override
	public void setContext(Context context) {
		// TODO Auto-generated method stub
		this.context=context;
	}
	class HandlerMessage extends Handler{

		@Override
		public void handleMessage(android.os.Message msg) {
			// TODO Auto-generated method stub
			Object obj=msg.obj;
			if(obj instanceof Message){
				Message message=(Message) msg.obj;
				Log.e("Test", message.getType());
				process(message);
			}		
			
		}
		public void process(Message message){
			if(message.getType().equals(Message.MESSAGE_LOGIN) && message.getStatus().equals(Message.OK)){
				Log.e("Start", message.getStatus()+":"+message.getType());
				Intent intent=new Intent(context, LoginSuccessActivity.class);
				context.startActivity(intent);
				((Activity)context).finish();
			}
			else if(message.getType().equals(Message.MESSAGE_LOGIN) && message.getStatus().equals(Message.FAIL)){
				Toast.makeText(context,"Login "+ message.getStatus(), Toast.LENGTH_LONG).show();
			}
			else if(message.getType().equals(Message.MESSAGE_LOGOUT) && message.getStatus().equals(Message.OK)){
				Intent intent=new Intent(context, HomeActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				context.startActivity(intent);
			}
			else if(message.getType().equals(Message.MESSAGE_LOGOUT) && message.getStatus().equals(Message.FAIL)){
				Toast.makeText(context, "Logout "+message.getStatus(), Toast.LENGTH_LONG).show();
			}			
		}
		
	}
}
